/**
 * 导出所有模型
 */

import Tenant from './Tenant.model';
import Plan from './Plan.model';
import Company from './Company.model';
import Department from './Department.model';
import User from './User.model';
import Role from './Role.model';
import Permission from './Permission.model';
import RolePermission from './RolePermission.model';
import PlanPermission from './PlanPermission.model';
import TenantPermission from './TenantPermission.model';
import AuditLog from './AuditLog.model';
import LoginLog from './LoginLog.model';
import Menu from './Menu.model';
import { DictionaryType, DictionaryItem } from './Dictionary.model';
import File from './File.model';
import SystemConfig from './SystemConfig.model';
import OssConfig from './OssConfig.model';

// 配置模型关联

// ========== 租户 & 套餐 ==========
Tenant.belongsTo(Plan, { foreignKey: 'planId', as: 'plan' });
Plan.hasMany(Tenant, { foreignKey: 'planId', as: 'tenants' });

// ========== 组织架构 ==========

// 租户 <-> 公司
Tenant.hasMany(Company, { foreignKey: 'tenantId', as: 'companies' });
Company.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });

// 公司 <-> 部门
Company.hasMany(Department, { foreignKey: 'companyId', as: 'departments' });
Department.belongsTo(Company, { foreignKey: 'companyId', as: 'company' });

// 部门自关联（树形结构）
Department.belongsTo(Department, { foreignKey: 'parentId', as: 'parent' });
Department.hasMany(Department, { foreignKey: 'parentId', as: 'children' });

// 部门 <-> 负责人
Department.belongsTo(User, { foreignKey: 'managerId', as: 'manager' });

// ========== 用户关联 ==========

// 用户 <-> 租户
User.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
Tenant.hasMany(User, { foreignKey: 'tenantId', as: 'users' });

// 用户 <-> 部门（通过部门可以获取公司信息）
User.belongsTo(Department, { foreignKey: 'departmentId', as: 'department' });
Department.hasMany(User, { foreignKey: 'departmentId', as: 'users' });

// 用户 <-> 角色
User.belongsTo(Role, { foreignKey: 'roleId', as: 'role' });
Role.hasMany(User, { foreignKey: 'roleId', as: 'users' });

Role.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
Tenant.hasMany(Role, { foreignKey: 'tenantId', as: 'roles' });

// 角色 <-> 权限（多对多）
Role.belongsToMany(Permission, { through: RolePermission, foreignKey: 'roleId', otherKey: 'permissionId', as: 'permissions' });
Permission.belongsToMany(Role, { through: RolePermission, foreignKey: 'permissionId', otherKey: 'roleId', as: 'roles' });

// 套餐 <-> 权限（多对多）
Plan.belongsToMany(Permission, { through: PlanPermission, foreignKey: 'planId', otherKey: 'permissionId', as: 'permissions' });
Permission.belongsToMany(Plan, { through: PlanPermission, foreignKey: 'permissionId', otherKey: 'planId', as: 'plans' });

// 租户 <-> 权限（多对多）
Tenant.belongsToMany(Permission, { through: TenantPermission, foreignKey: 'tenantId', otherKey: 'permissionId', as: 'permissions' });
Permission.belongsToMany(Tenant, { through: TenantPermission, foreignKey: 'permissionId', otherKey: 'tenantId', as: 'tenants' });

// 租户权限 -> 授权人
TenantPermission.belongsTo(User, { foreignKey: 'grantedBy', as: 'granter' });

// 登录日志 -> 用户
LoginLog.belongsTo(User, { foreignKey: 'userId', as: 'user' });
LoginLog.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });

// 操作日志 -> 用户
AuditLog.belongsTo(User, { foreignKey: 'userId', as: 'user' });
AuditLog.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });

// 菜单自关联
Menu.belongsTo(Menu, { foreignKey: 'parentId', as: 'parent' });
Menu.hasMany(Menu, { foreignKey: 'parentId', as: 'children' });

// ========== 文件关联 ==========
File.belongsTo(User, { foreignKey: 'userId', as: 'user' });
User.hasMany(File, { foreignKey: 'userId', as: 'files' });

File.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
Tenant.hasMany(File, { foreignKey: 'tenantId', as: 'files' });

// ========== 配置关联 ==========
SystemConfig.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
Tenant.hasMany(SystemConfig, { foreignKey: 'tenantId', as: 'configs' });

export {
  Tenant,
  Plan,
  Company,
  Department,
  User,
  Role,
  Permission,
  RolePermission,
  PlanPermission,
  TenantPermission,
  AuditLog,
  LoginLog,
  File,
  Menu,
  DictionaryType,
  DictionaryItem,
  SystemConfig,
  OssConfig,
};

export default {
  Tenant,
  Plan,
  Company,
  Department,
  User,
  Role,
  Permission,
  RolePermission,
  PlanPermission,
  TenantPermission,
  AuditLog,
  LoginLog,
  File,
  Menu,
  DictionaryType,
  DictionaryItem,
  SystemConfig,
  OssConfig,
};



